package com.xunheyun.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xunheyun.service.IConfSyncService;
import com.xunheyun.service.IServerService;
import com.xunheyun.vo.ConfigFile;
import com.xunheyun.vo.ConfigService;
import com.xunheyun.vo.HostServer;
import com.xunheyun.vo.UserForm;

/**
 * 配置同步Controller
 * @author zhanght
 */
@Controller
@RequestMapping(value="/confsync")
public class ConfSyncController {

	@Autowired
	private IConfSyncService confSyncService;
	
	@Autowired
	private IServerService serverService;
	
	@RequestMapping(value="sync")
	@ResponseBody
	public String sync(HttpServletRequest request){
		
		String configId = request.getParameter("configId");	// 配置ID
		String hosts = request.getParameter("hosts");		// 主机列表
		
		String result = confSyncService.syncConfig(configId, hosts);
		
		return result;
	}
	
	/**
	 * 获取配置列表
	 * @return
	 */
	@RequestMapping(value="configServiceList")
	public String configServiceList(HttpServletRequest request){
		
		UserForm user = (UserForm) request.getSession().getAttribute("user");
		
		int user_id = user.getUser_id();
		
		List<ConfigService> serviceList = confSyncService.configServiceList(user_id);
		
		request.setAttribute("services", serviceList);
		request.setAttribute("fail", "");
		
		return "config_service_list";
	}
	/**
	 * 添加服务配置页面
	 * @return
	 */
	@RequestMapping(value="addConfigService")
	public String addConfigServicePage(HttpServletRequest request){
		
		request.setAttribute("fail", "");
		
		return "config_service_add";
	}
	
	/**
	 * 添加服务配置
	 * @return
	 */
	@RequestMapping(value="addcs")
	public String addConfigService(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("form") ConfigService configService){
		
		UserForm user = (UserForm) request.getSession().getAttribute("user");
		configService.setUser_id(user.getUser_id());
		
		confSyncService.addConfigService(configService);
		
		request.setAttribute("fail", "");
		
		return "redirect:/confsync/configServiceList";
	}
	
	@RequestMapping(value="/editpage/{configServiceId}")
	public String editConfigServicePage(HttpServletRequest request,@PathVariable int configServiceId){
		
		ConfigService configService = confSyncService.getConfigServiceById(configServiceId);
		
		request.setAttribute("configService", configService);
		
		return "config_service_edit";
	}
	
	@RequestMapping(value="/updateConfigService")
	public String updateConfigService(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("form") ConfigService configService){
		
		confSyncService.updateConfigService(configService);
		
		return "redirect:/confsync/configServiceList";
	}
	
	/**
	 * 删除配置服务
	 * @param request
	 * @return
	 */
	@RequestMapping(value="deleteConfigService")
	public String deleteConfigService(HttpServletRequest request){
		
		String configServiceId = request.getParameter("configServiceId");
		
		confSyncService.deleteConfigService(Integer.valueOf(configServiceId));
		
		return "redirect:/confsync/configServiceList";
	}
	
	@RequestMapping(value="/configFileList")
	public String configFileListPage(HttpServletRequest request){
		
		String configServiceId = request.getParameter("configServiceId");
		
		List<ConfigFile> fileList = confSyncService.configFileList(Integer.valueOf(configServiceId));
		
		// 获取当前配置主机列表
		List<HostServer> list = serverService.list(Integer.valueOf(configServiceId));
				
		request.setAttribute("hosts", list);
		request.setAttribute("configs", fileList);
		request.setAttribute("configServiceId", configServiceId);
		
		return "config_file_list";
	}
	
	@RequestMapping(value="/createFile")
	public String createConfigFile(HttpServletRequest request){
		
		return "config_file_editor";
	}
	
	@RequestMapping(value="/saveConfigFile")
	@ResponseBody
	public String saveConfigFile(HttpServletRequest request){
		
		String configServiceId = request.getParameter("configServiceId");
		String content = request.getParameter("content");
		String fileName = request.getParameter("configFileName");
		String configId = request.getParameter("configId");
		String filePath = request.getParameter("filePath");
		
		confSyncService.saveConfigFile(configServiceId, content, fileName,configId,filePath);
		
		return "success";
	}
	
	@RequestMapping(value="/deleteFile")
	@ResponseBody
	public String deleteConfigFile(HttpServletRequest request){
		
		String configId = request.getParameter("configId");
		
		confSyncService.deleteConfigFile(configId);
		
		 return "success";
	}
	
	@RequestMapping(value="/getConfigFile")
	public String getConfigFile(HttpServletRequest request){
		
		String configId = request.getParameter("configId");
		
		ConfigFile configFile = confSyncService.getConfigFileById(Integer.valueOf(configId));
		
		request.setAttribute("configContent", configFile.getConfig_content());
		request.setAttribute("configId", configFile.getConfig_id());
		request.setAttribute("configName", configFile.getConfig_name());
		request.setAttribute("filePath", configFile.getFile_path());
		
		return "config_file_editor_update";
	}
}




